/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* h = new ListNode();
        h->next = head;

        ListNode* t = h;
        ListNode* temp;
        while(t->next != nullptr){
            if(t->next->val == val){
                temp = t->next;
                t->next = t->next->next;
                delete(temp);
            } else {
                t = t->next;
            }
        }
        return h->next;
    }
};